package com.platform.service.impl;

import com.platform.cache.J2CacheUtils;
import com.platform.dao.ProStyleDao;
import com.platform.dao.ProductDao;
import com.platform.entity.ProStyleEntity;
import com.platform.entity.ProductEntity;
import com.platform.service.ProductService;
import com.platform.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program:
 * @Description: 编辑商品:所有商品
 * @Author: liweihai
 * @Date: Created in 2019/1/31 17:10
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private ProStyleDao proStyleDao;


    @Override
    public ProductEntity queryObject(Integer id) {
        ProductEntity productEntity = productDao.queryObject(id);
        return productEntity;
    }

    @Override
    public List<ProductEntity> queryList(Map<String, Object> map) {
        map.put("merchantId",1);
        List<ProductEntity> list = productDao.queryList(map);
        return list;
    }
    //////商品审核
    @Override
    public List<ProductEntity> queryListByParam(Map<String, Object> map) {
        return productDao.queryListByParam(map);
    }

    @Override
    public Integer queryTotalByParam(Map<String, Object> param) {
        return productDao.queryTotalByParam(param);
    }

    @Override
    public Integer updateProductAudit(Integer id, Integer version, Integer status) {
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("version",version);
        param.put("id",id);
        param.put("auditStatus",status);
        return productDao.updateProductAudit(param);
    }

    @Override
    public ProductEntity queryObjectByParam(Integer id) {
        ProductEntity productEntity = productDao.queryObject(id);
        Long pid = productEntity.getId();
        Map<String ,Object> map = new HashMap<>();
        map.put("pid", pid);
        map.put("merchant_id",productEntity.getMerchantId());
        //根据ID查询款式列表
        List<ProStyleEntity> proStyleEntities = proStyleDao.queryAuditStyles(map);
        productEntity.setProStyles(proStyleEntities);
        return productEntity;
    }

    @Override
    public Integer saveAudit(ProductEntity productEntity) {
        int save = productDao.saveAudit(productEntity);
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        return save;
    }

    @Override
    public Integer updateAudit(ProductEntity productEntity) {
        int update = productDao.update(productEntity);
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        return update;
    }


    //////

    @Override
    public int queryTotal(Map<String, Object> map) {
        map.put("merchantId",1);
        return productDao.queryTotal(map);
    }

    @Override
    public int save(ProductEntity tag) {
            tag.setMerchantId(1);
            tag.setMerchantName("动动体育");
        int save = productDao.save(tag);
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        return save;
    }

    @Override
    public int update(ProductEntity tag) {
            tag.setMerchantId(1);
            tag.setMerchantName("动动体育");
        int update = productDao.update(tag);
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        return update;
    }



    @Override
    public int delete(Integer id) {
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        int delete = productDao.delete(id);
        return delete;
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        int deleteBatch = productDao.deleteBatch(ids);
        return deleteBatch;
    }

    @Override
    public List<ProductEntity> queryObjectByMId(Integer merchantId) {
        return productDao.queryObjectByMId(merchantId);
    }

    @Override
    public int deleteMerchantPro(Integer merchantId) {
        List<ProductEntity> products = productDao.queryObjectByMId(merchantId);
        List<Long> ids = products.stream().map(ProductEntity::getId).collect(Collectors.toList());
        productDao.deleteBatch(ids.toArray());
        return  proStyleDao.deleteMerchantBatch(ids);
    }
}
